﻿<%@ Page Title="" Language="C#" MasterPageFile="~/admin/SiteAdmin.Master" AutoEventWireup="true" CodeBehind="ht_sxsp.aspx.cs" Inherits="Andy.Web.admin.ht_sxsp" %>
<%@ Register TagPrefix="cc1" Namespace="Vdc.Grid" Assembly="vdc_Grid_NET" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <script type="text/javascript">

        var rowPrefix;

        // find out the row Prefix 
        function getRowPrefix()
        {
            var row0 = grid1.getRecordsIds().split(",")[0];
			
            if ( row0 != null)
            {
                var lastPos = -1;
                for ( var i = row0.length-1; i>=0; i--)
                {
                    if ( row0.charAt(i)== '_' )
                    {
                        lastPos = i;
                        break;
                    }
                }

                if (lastPos > -1)
                {
                    // get Prefix
                    return row0.substring(0,lastPos + 1);
                }
            }

			

            return "";
        }


        function moveRowUp() {
            if (rowPrefix == null || rowPrefix == "")
            {
                rowPrefix = getRowPrefix();
            }

            var index = null;

            if ( grid1.SelectedRecords[0] == null ) {
                alert("Please select a record.");
                return;
            }
            for(var i=0; i<grid1.Rows.length; i++) {
                if (grid1.Rows[i].Cells[0].Value == grid1.SelectedRecords[0].Id) {
                    index = i;
                    break;
                }
            }

            if( index != null) {
                var rowMove = document.getElementById(rowPrefix + index );
                var rowPre = rowMove.previousSibling;
                if ( rowMove != null && rowPre != null)
                {
                    rowMove.parentNode.insertBefore( rowMove, rowPre);
			
                    swap( rowPre, rowMove, index, index - 1 );
                }
				
            }
        }

        function moveRowDown() {
            if (rowPrefix == null || rowPrefix=="")
            {
                rowPrefix = getRowPrefix();
            }
            var index = null;

            if ( grid1.SelectedRecords[0] == null ) {
                alert("Please select a record.");
                return;
            }
            for(var i=0; i<grid1.Rows.length; i++) {
                if(grid1.Rows[i].Cells[0].Value == grid1.SelectedRecords[0].Id) {
                    index = i;
                    break;
                }
            }

            if( index != null) {
                var rowMove = document.getElementById(rowPrefix + index );
                var rowNext = rowMove.nextSibling;
                if ( rowMove != null && rowNext != null)
                {
                    rowMove.parentNode.insertBefore( rowNext, rowMove);
			
                    swap( rowNext, rowMove, index, index + 1 );
                }
				
            }
        }
		
        function swap( row1, row2, idx1, idx2){
            // swap Edit & Delete Link
            var div1 = row1.lastChild.firstChild;
            var div2 = row2.lastChild.firstChild;
			
            var edit1 = div1.firstChild;
            var delete1 = div1.lastChild;

            var edit2 = div2.firstChild;
            var delete2 = div2.lastChild;

            var tmp = edit1.onclick;
            edit1.onclick = edit2.onclick;
            edit2.onclick = tmp;

            tmp = delete1.onclick;
            delete1.onclick = delete2.onclick;
            delete2.onclick = tmp;

            // swap row ID
            tmp = row1.id;
            row1.id = row2.id;
            row2.id = tmp;

            // swap grid data;
            tmp = grid1.Rows[idx1];
            grid1.Rows[idx1] = grid1.Rows[idx2];
            grid1.Rows[idx2] = tmp;

            //swap grid row position
            tmp = grid1.Rows[idx1].Cells[1].Value ;
            grid1.Rows[idx1].Cells[1].Value  = grid1.Rows[idx2].Cells[1].Value ;
            grid1.Rows[idx2].Cells[1].Value  = tmp;


        }
        function updateGrid(gridRows) {
            //update to DB
            var records = "";
            for (var i = 0; i < gridRows.length; i++) {
                records += gridRows[i].Cells[0].Value + "," + gridRows[i].Cells[1].Value + "|";
            }

            ob_post.AddParam("records", records);
            ob_post.post(null, "saveRowPosition", handlingUpdate);
        }

        function handlingUpdate(effectedRecords) {
            if (effectedRecords != null) {
                alert('Cập nhật ' + effectedRecords);
            } 

        }

        </script>	
    <div id="page">
        <div class="inner">
            <div class="section table_section">
                <!--[if !IE]>start title wrapper<![endif]-->
                <div class="title_wrapper">
                    <h2>Sắp xếp sản phẩm nổi bật</h2>
                    <ul class="section_menu">

                        <li><a href="ht_qlsp.aspx"><span><span>Quản lý sản phẩm</span></span></a></li>
                        <li><a class="active" href="#null"><span><span>Sắp xếp sản phẩm</span></span></a></li>
                        <li><a href="ht_themsp.aspx"><span><span>Thêm mới sản phẩm</span></span></a></li>
                    </ul>
                    <span class="title_wrapper_left"></span><span class="title_wrapper_right"></span>
                </div>
                <!--[if !IE]>end title wrapper<![endif]-->
                <!--[if !IE]>start section content<![endif]-->
                <div class="section_content">
                    <!--[if !IE]>start section content top<![endif]-->
                    <div class="sct">
                        <div class="sct_left">
                            <div class="sct_right">
                                <div class="sct_left">
                                    <div class="sct_right">

                                        <fieldset>
                                            <!--[if !IE]>start table_wrapper<![endif]-->
                                            <div class="table_wrapper">
                                                <div class="table_wrapper_inner">
                                                    <div style="border-bottom: 1px solid #D3E5ED; padding-left: 10px; height: 40px; padding-top: 15px">
                                                        
                                                        <p style="float: left; margin-left: 15px;">
                                                            <input type="button" onclick="moveRowUp()" value="Lên" class="tdText" />
		                                                <input type="button" onclick="moveRowDown()" value="Xuống" class="tdText" />
		                                                <input type="button" onclick="updateGrid(grid1.Rows);" value="Lưu" class="tdText" />
                                                        </p>
                                                    </div>

                                                </div>
                                            </div>
                                        </fieldset>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="grid">
                    <cc1:Grid ID="grid1" runat="server" CallbackMode="false" Serialize="true" AllowAddingRecords="true" PageSize="-1"
                        FolderStyle="~/styles/premiere_blue" AutoGenerateColumns="false" AllowMultiRecordSelection="false" Width="100%"
                        ShowFooter="true" AllowSorting="false" AllowPaging="false" AllowPageSizeSelection="false" GenerateRecordIds="true">
                        <PagingSettings ShowRecordsCount="false" />
                        <TemplateSettings NewRecord_TemplateId="ButtonsTemplate" />
                        <Columns>
                            <cc1:Column ID="Column2" DataField="Id" HeaderText="ID" runat="server" Visible="false" />
                            <cc1:Column ID="Column1" DataField="ViTri" HeaderText="Vị trí" runat="server" Visible="false" />
                            <cc1:Column ID="Column5" DataField="RowNumber" HeaderText="Vị trí" runat="server" />
                            <cc1:Column ID="Column3" DataField="Ten" HeaderText="Ten" runat="server" />
                            <cc1:Column ID="Column4" DataField="TenLoai" HeaderText="TenLoai" runat="server" />
                            <cc1:Column DataField="Anh" HeaderText="Ảnh">
                                <TemplateSettings TemplateId="ImageSanPham" />
                            </cc1:Column>
                        </Columns>
                        <Templates>
                            <cc1:GridTemplate runat="server" ID="ImageSanPham">
                                <Template>
                                        <img src="../<%# Container.DataItem["Anh"] %>" width="100" height="100" title="" alt="" />
                                    </Template>
                            </cc1:GridTemplate>
                        </Templates>
                                
                    </cc1:Grid>
                </div>
            </div>

        </div>

    </div>
</asp:Content>
